package com.qianfeng.wuyang.himalaya.adapter.recommendadapter;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qianfeng.wuyang.himalaya.R;
import com.qianfeng.wuyang.himalaya.activity.MainActivity;
import com.qianfeng.wuyang.himalaya.activity.MyApplication;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.GuessData;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.LikeEntity;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.TopEntity;
import com.squareup.picasso.Picasso;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by WangQing on 2016/12/9.
 */

public class RecommendGuessAdapter extends RecyclerView.Adapter<RecommendGuessAdapter.ViewHolder> implements View.OnClickListener, Callback<TopEntity>, View.OnTouchListener {
    private Context context;
    private List<LikeEntity> list;
    private MainActivity activity;
    private GuessData data;
    RecyclerView recyclerView_detail,recyclerView_pro;
    View view_drawer;
    ViewPager viewpager;

    public RecommendGuessAdapter(Context context, List<LikeEntity> list, MainActivity activity) {
        this.context = context;
        this.list = list;
        this.activity = activity;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_recommend_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (list.get(position).getTitle() != null) {
            holder.title.setText(list.get(position).getTags());
        }
        if (list.get(position).getCoverLarge() != null) {
            Picasso.with(context).load(list.get(position).getCoverLarge()).into(holder.img);
        }
        holder.author.setText(list.get(position).getTitle());

        holder.layout.setOnClickListener(this);

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View view) {
//        int position=((RecyclerView)view.getParent().getParent().getParent()).getChildAdapterPosition(view);
        int position = ((RecyclerView) view.getParent()).getChildAdapterPosition(view);
        activity.drawerLayout.openDrawer(Gravity.RIGHT);

        FrameLayout frameLayout = (FrameLayout) activity.findViewById(R.id.drawer_container);
        view_drawer = LayoutInflater.from(context).inflate(R.layout.recomend_guess2, frameLayout, false);
        frameLayout.addView(view_drawer);

        ImageView imaeg_back = (ImageView) view_drawer.findViewById(R.id.drawer_back);
        imaeg_back.setOnClickListener(this);
        if (view.getId() == R.id.drawer_back) {
            activity.drawerLayout.closeDrawers();
        }
        //加载抽屉里面的头部是图
        intdrawer(position);

    }

    private void intdrawer(int position) {

        TextView text_title, text_nickname, text_playCounts, text_timeUp;
        ImageView image_guess2_head;
        text_title = (TextView) view_drawer.findViewById(R.id.text_data_guess2_title);
        text_timeUp = (TextView) view_drawer.findViewById(R.id.text_recomend_guess2_timeUp);
        text_playCounts = (TextView) view_drawer.findViewById(R.id.text_recomend_guess2playsCounts);
        text_nickname = (TextView) view_drawer.findViewById(R.id.text_recomend_guess2_nickname);
        image_guess2_head = (ImageView) view_drawer.findViewById(R.id.image_recomend_guess2_head);


        text_nickname.setText("主播：" + list.get(position).getNickname());
        text_playCounts.setText("播放：" + (int) list.get(position).getPlaysCounts() / 10000 + "万次");
        text_title.setText(list.get(position).getTitle());
        SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd", Locale.getDefault());
        text_timeUp.setText("状态：" + format.format(new Date(System.currentTimeMillis())));
        Picasso.with(context).load(list.get(position).getCoverLarge()).into(image_guess2_head);


        viewpager = (ViewPager) view_drawer.findViewById(R.id.recomend_guess2_viewpager);


        View view_detail = LayoutInflater.from(context).inflate(R.layout.fragment_recomend_progrem, null);
        View view_progrem = LayoutInflater.from(context).inflate(R.layout.fragment_recomend_progrem, null);

        //详情页的布局
        recyclerView_detail = (RecyclerView) view_detail.findViewById(R.id.recomend_progrem_recycler);
        MyApplication application = (MyApplication) activity.getApplication();
        application.getDetailService().getdata("3523862").enqueue(this);


        //节目页
        recyclerView_pro= (RecyclerView) view_progrem.findViewById(R.id.recomend_progrem_recycler);


        List<View> list_view = new ArrayList<>();
        list_view.add(view_detail);
        list_view.add(view_progrem);
        Adapter_Guess2_proPage adapter = new Adapter_Guess2_proPage(list_view);
        viewpager.setAdapter(adapter);
        viewpager.setOnTouchListener(this);
    }
    //解决viewpager和抽屉的滑动冲突
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (motionEvent.getX() > view.getWidth() / 8) {
                    viewpager.requestDisallowInterceptTouchEvent(true);
                }
                break;
        }
        return false;
    }

    @Override
    public void onResponse(Call<TopEntity> call, Response<TopEntity> response) {
        if (response.body() != null) {

            if (response.body().getData() != null) {
                data = response.body().getData();
                Adapter_Rec2Detail adapter_rec2Detail = new Adapter_Rec2Detail(context, data);
                recyclerView_detail.setAdapter(adapter_rec2Detail);
            }
        }
    }

    @Override
    public void onFailure(Call<TopEntity> call, Throwable t) {
        Toast.makeText(context, "下载失败", Toast.LENGTH_SHORT).show();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final ImageView img;
        private final TextView title;
        private final TextView author;
        private LinearLayout layout;

        public ViewHolder(View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.recommend_item_img);
            title = (TextView) itemView.findViewById(R.id.recommend_item_title);
            author = (TextView) itemView.findViewById(R.id.recommend_item_author);
            layout = (LinearLayout) itemView.findViewById(R.id.recommend_item_layout);

        }
    }
}
